# Copyright (C) 2006  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

#
# For more information visit http://carrierclass.net/
#
package CCN::Class;
use strict; use warnings;
use base qw(CCN::Object);
use Carp;

sub initialize()
{
    my $self = shift;
    $self->{success} = 1;
}

our $logger;
our $success;

sub success()   { return $success; }

sub set_logger($)
{
    my $self = shift;
    $logger = shift;
}

sub error
{
    my $self = shift;
    $self->log_for('err',@_);
    $success = 0;
}

sub warn
{
    my $self = shift;
    $self->log_for('warning',@_);
}

sub cluck
{
    my $self = shift;
    use Carp;
    $self->warn(@_,"\n",Carp::longmess);
}

sub log
{
    my $self = shift;
    $self->log_for('notice',@_);
}

sub debug
{
    my $self = shift;
    $self->log_for('debug',@_);
}

sub die
{
    my $self = shift;
    $self->error(@_,"\n",Carp::longmess);
    $self->finish;
}

sub finish()
{
    my $self = shift;
    exit ($success ? 0 : 1);
}

# $level should be a syslog-level-compatible name (eg "err", "info", "warn").
sub log_for
{
    my $self = shift;
    my $level = shift;

    if(defined $logger)
    {
        $level = lc($level);
        $logger->log_for($level,@_);
    }
    else
    {
        my $timestamp = scalar localtime;
        $level = uc($level);
        print STDERR "$timestamp CCN-$level: ".join(' ',@_);
    }
}

1;